package com.pets.common.utils;

import cn.hutool.core.util.URLUtil;
import com.pets.common.utils.sign.Md5Utils;
import com.pets.common.utils.uuid.UUID;

import java.util.List;
import java.util.stream.Collectors;

public class CDNUtil {

    private static final String KEY = "adccf2d664e270da18d65d2d10669fd6";

    public static List<String> signs(List<String> urls){
        return urls.stream().map(CDNUtil::sign).collect(Collectors.toList());
    }

    public static String sign(String url) {
        int userId = 0;
        String path = URLUtil.getPath(url);
        String uuid = UUID.randomUUID().toString(true);
        long second = System.currentTimeMillis()/1000;
        long ttl = second + 60;
        String str = String.format("%s-%s-%s-%s-%s", path, ttl, uuid, userId, KEY);
        String hash = Md5Utils.hash(str);
        return String.format("%s?auth_key=%s-%s-%s-%s", url, ttl, uuid, userId, hash);
    }
}
